<template>
  <div class="login_content">
    <div class="login_box">
      <div class="acatar_box">
        <img src="@/assets/acatar.jpg" alt="" />
      </div>
      <!--登录表单区域 -->
      <el-form
        ref="loginForm"
        class="login_form"
        :model="loginForm"
        :rules="rules"
      >
        <!-- 用户名  -->
        <el-form-item prop="username">
          <el-input
            prefix-icon="el-icon-user"
            v-model="loginForm.username"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            prefix-icon="el-icon-lock"
            v-model="loginForm.password"
            type="password"
          ></el-input>
        </el-form-item>
        <el-form-item class="login_btn">
          <el-button type="primary" @click="loginFn">登录</el-button>
          <el-button type="info" @click="resetFn">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 表单数据绑定
      loginForm: {
        username: "admin",
        password: "123456",
      },
      // 表单验证
      rules: {
        // 通过prop属性 -- 用户名校验
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 6, message: "长度在 3 到 6 个字符", trigger: "blur" },
        ],
        //// 通过prop属性 -- 密码校验
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 6, max: 16, message: "密码在6~16位", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    // 重置按钮
    resetFn() {
      // console.log(this.$refs.loginForm);
      this.$refs.loginForm.resetFields();
    },
    // 点击登录
    loginFn() {
      // 表单预检
      this.$refs.loginForm.validate(async (valild) => {
        if (!valild) return;
        // 发送axios请求
        const { data: res } = await this.$axios({
          method: "post",
          url: "login",
          data: this.loginForm,
        });
        if (res.meta.status !== 200) return this.$message.error("登录失败！");
        // console.log(res);
        window.sessionStorage.setItem("token", res.data.token);
        this.$message.success("登录成功");
        this.$router.push("/home");
      });
    },
  },
};
</script>

<style lang="less" scoped>
.login_content {
  height: 100%;
  background-color: rgba(209, 172, 201, 0.5);
}
.login_box {
  width: 450px;
  height: 300px;
  background-color: rgba(255, 255, 255, 0.36);
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);

  .acatar_box {
    width: 130px;
    height: 130px;
    border-radius: 50%;
    padding: 10px;
    border: 1px solid #eee;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;
    box-shadow: 0 0 10px rgb(219, 196, 196);
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }
}

.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
  .login_btn {
    display: flex;
    justify-content: flex-end;
  }
}
</style>
